using System;
using System.Data;
using System.Windows.Forms;
using Syncfusion.Windows.Forms.Grid;
using ctsu.BaseForms;
using ctsu.Admin.Forms;


namespace ctsu.Admin.BusLogic.CoreLab
{
	public class BLCoreLabFormGridDrawLabels : BLCoreLabFormGridBaseImpl
	{
		public BLCoreLabFormGridDrawLabels(FormGridMeta formGridMeta, GridControl grid, FormInfoBase formInfo, Form form)
			: base(formGridMeta, grid, formInfo, form)
		{

		}

		protected override void Grid_CurrentCellChanged(object sender, EventArgs e)
		{
			base.Grid_CurrentCellChanged(sender, e);
			string message = "";
			GridCurrentCell cc = this._grid.CurrentCell;
			//protocol
			if (cc.ColIndex == 2)
			{
				int id = (int)cc.Renderer.ControlValue;
				ProtocolInfo pi = CoreLabUtils.GetProtocolInfo(id, out message);
				this._grid[cc.RowIndex, 2].Tag = pi;
				this._grid[cc.RowIndex, 2].CellTipText = pi.GetToolTip();
			}
			//protocol
			if (cc.ColIndex == 3)
			{
				int id = (int)cc.Renderer.ControlValue;
				PatientInfo pi = CoreLabUtils.GetPatientInfo(id, out message);
				this._grid[cc.RowIndex, 3].Tag = pi;
				this._grid[cc.RowIndex, 3].CellTipText = pi.GetToolTip();
			}
		}

		#region add replace remove
		public override void ReplaceRow(object obj, int row)
		{
			DataRow dr = (DataRow)this._grid[row, 0].Tag;
			Sample samp = (Sample)obj;

			if (samp.Protocol > 0)
			{
				this._grid[row, 2].CellValue = samp.Protocol;
				this._grid[row, 2].Tag = samp.ProtocolInfo;
				this._grid[row, 2].CellTipText = samp.ProtocolInfo.GetToolTip();
				AssignChangeToDataRow(row, 2);
			}
			if (samp.PatientID > 0)
			{
				this._grid[row, 3].CellValue = samp.PatientID;
				this._grid[row, 3].Tag = samp.PatientInfo;
				this._grid[row, 3].CellTipText = samp.PatientInfo.GetToolTip();
				AssignChangeToDataRow(row, 3);
			}
			if (samp.VisitDate.Ticks > 0)
			{
				this._grid[row, 4].CellValue = samp.VisitDate;
				this._grid[row, 4].Format = "MM/dd/yyyy";
				AssignChangeToDataRow(row, 4);
			}
			if (samp.Visit.Length > 0)
			{
				this._grid[row, 5].CellValue = samp.Visit;
				AssignChangeToDataRow(row, 5);
			}
			if (samp.VisitTime.Length > 0)
			{
				this._grid[row, 6].CellValue = samp.VisitTime;
				AssignChangeToDataRow(row, 6);
			}

			if (samp.SampleType > -1)
			{
				this._grid[row, 7].CellValue = samp.SampleType;
				AssignChangeToDataRow(row, 7);
			}
			if (samp.SmallNote.Length > 0)
			{
				this._grid[row, 8].CellValue = samp.SmallNote;
				AssignChangeToDataRow(row, 8);
			}

			this._btnEditBatch.Enabled = true;
			this._btnRemoveBatch.Enabled = true;
			this._btnSave.Enabled = true;
		}

		public override void AddRow(object obj, int row)
		{
			Sample samp = (Sample)obj;
			DataRow dr = _data.NewRow();
			this._data.Rows.Add(dr);

			int rows = ++this._grid.RowCount;

			this._grid[rows, 0].Tag = dr;

			this._grid[rows, 2].CellValue = samp.Protocol;
			this._grid[rows, 2].Tag = samp.ProtocolInfo;
			this._grid[rows, 2].CellTipText = samp.ProtocolInfo.GetToolTip();
			AssignChangeToDataRow(rows, 2);

			this._grid[rows, 3].CellValue = samp.PatientID;
			this._grid[rows, 3].Tag = samp.PatientInfo;
			this._grid[rows, 3].CellTipText = samp.PatientInfo.GetToolTip();
			AssignChangeToDataRow(rows, 3);

			this._grid[rows, 4].CellValue = samp.VisitDate;
			this._grid[rows, 4].Format = "MM/dd/yyyy";
			AssignChangeToDataRow(rows, 4);
			this._grid[rows, 5].CellValue = samp.Visit;
			AssignChangeToDataRow(rows, 5);
			this._grid[rows, 6].CellValue = samp.VisitTime;
			AssignChangeToDataRow(rows, 6);
			this._grid[rows, 7].CellValue = samp.SampleType;
			AssignChangeToDataRow(rows, 7);
			this._grid[rows, 8].CellValue = samp.SmallNote;
			AssignChangeToDataRow(rows, 8);

			base.AddRow(samp, row);
		}
		#endregion add replace remove

		#region display data
		protected override void DisplayRow(int row, DataRow dr)
		{
			string message = "";
			for (int i = 1; i < this._grid.ColCount + 1; i++)
			{
				DataColumn dc = (DataColumn)this._grid[0, i].Tag;

				if (this._grid[row, i].CellType == "CheckBox")
				{
					if (dr[dc.ColumnName] != System.DBNull.Value)
					{
						if ((bool)dr[dc.ColumnName])
							this._grid[row, i].CellValue = "1";
						else
							this._grid[row, i].CellValue = "0";
					}
				}
				else
					this._grid[row, i].CellValue = dr[dc.ColumnName];
				if (this._grid[row, i].CellType == "MonthCalendar")
					this._grid[row, i].Format = "MM/dd/yyyy";

				if (dc.ColumnName == "Protocol")
				{
					int protocol = Convert.ToInt32(dr[dc.ColumnName]);
					ProtocolInfo pi = CoreLabUtils.GetProtocolInfo(protocol, out message);
					this._grid[row, i].Tag = pi;
					if (pi.ID > -1)
						this._grid[row, i].CellTipText = pi.GetToolTip();
				}
				if (dc.ColumnName == "Patient")
				{
					int patient = Convert.ToInt32(dr[dc.ColumnName]);
					PatientInfo pi = CoreLabUtils.GetPatientInfo(patient, out message);
					this._grid[row, i].Tag = pi;
					if (pi.ID > -1)
						this._grid[row, i].CellTipText = pi.GetToolTip();
				}
			}
		}
		#endregion display data
	}
}
